#include "report_config.h"
u32 rec_message;
static u32 message;
u16 send_num=0;
u8 buffer[SIZE];
/******************回包用的messageId************************/
void set_rec_message(u8 *buf)
{
    memcpy(&rec_message,buf,4);
}

u32 get_rec_message()
{
    u32 i;
    i=rec_message;
    rec_message=0;
    return i;
}

/******************主动上报用的messageId******************************************/
u32 get_message()
{
    u32 i;
    if (message == 0xffffffff)
    {
        message =0;
    }
    i=message;
    message++;
    return i;
}

extern char buf_device[17];
/*回包上报*/
void ACK_report()
{
    u16 num=0;
    u8 buf[1024];
    buf[0]=0xA5;
    memcpy(&buf[3],buf_device,16);
    num+=16;
    //MI
    u32 mess =get_rec_message();
    memcpy(&buf[19],&mess,4);
    num+=4;
    //指令码
    u32 code = ACK;
    memcpy(&buf[23],&code,4);
    num+=4;
    memcpy(&buf[1],&num,2);
    //CRC
    buf[27]=0;
    buf[28]=0;

    buf[29]=0x5a;
    TCP_send_data(buf,(num+1+1+2+2));
}





/*mac地址获取函数*/
void get_mac(u8 *buf)
{
    u8 mac[6];
    esp_err_t err = esp_read_mac(mac, ESP_MAC_WIFI_STA);
    if (ESP_OK == err)
    {
        char mac_str[18];
        snprintf(mac_str, sizeof(mac_str), "%02X:%02X:%02X:%02X:%02X:%02X",
        mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
        ESP_LOGI("MAC_INFO", "STA MAC Address: %s", mac_str);
    }
    else
    {
        ESP_LOGE("MAC_ERROR", "Failed to get STA MAC address. Error: %d", err);
    }
    memcpy(buf,mac,6*sizeof(uint8_t));
}


/*上报注册包*/
void send_register_package()
{   
    u16 num=0;
    u8 buf[1024];
    buf[0]=0xA5;
    memcpy(&buf[3],buf_device,16);  //deviceId
    num+=16;
    //MI
    u32 mess=get_message();
    memcpy(&buf[19],&mess,4); //messageId
    num+=4;
    //指令码
    memset(&buf[23],0,4);
    num+=4;
    //版本
    memcpy(&buf[27],SOFT_VER,3); //软件版本号
    num+=3;
    memcpy(&buf[30],FW_VER,3); //硬件版本号
    num+=3;
    memcpy(&buf[33],PROJECT_NAME,4); //PN
    num+=4;
    //硬件mac地址
    get_mac(&buf[37]);
    num+=6;
    //密钥
    memcpy(&buf[45],SECRET,strlen(SECRET)); //密钥
    num+=strlen(SECRET);
    u16 n =strlen(SECRET);
    memcpy(&buf[43],&n,2);
    num+=2;
    memcpy(&buf[1],&num,2);
    //CRC
    buf[51]=0;
    buf[52]=0;

    buf[53]=0x5a;

    TCP_send_data(buf,54);
    uart1_msg_send(buf,54);
}


/***********OTA回包*******************/
void OTA_report()
{
    u16 num=0;
    u8 buf[1024];
    buf[0]=0xA5;
    memcpy(&buf[3],buf_device,16);
    num+=16;
    //MI
    u32 mess =get_message();
    memcpy(&buf[19],&mess,4);
    num+=4;
    //指令码
    int i=OTA;
    memcpy(&buf[23],&i,4);
    num+=4;
    memcpy(&buf[1],&num,2);
    //CRC
    buf[27]=0;
    buf[28]=0;

    buf[29]=0x5a;
    TCP_send_data(buf,(num+1+1+2+2));
    uart1_msg_send(buf,(num+1+1+2+2));
}

/*************网络重置回包****************************/
void net_reset_report()
{
    u16 num=0;
    u8 buf[1024];
    buf[0]=0xA5;
    memcpy(&buf[3],buf_device,16);
    num+=16;
    //MI
    u32 mess =get_message();
    memcpy(&buf[19],&mess,4);
    num+=4;
    //指令码
    u32 code = NET_RESET;
    memcpy(&buf[23],&code,4);
    num+=4;
    memcpy(&buf[1],&num,2);
    //CRC
    buf[27]=0;
    buf[28]=0;

    buf[29]=0x5a;
    TCP_send_data(buf,(num+1+1+2+2));
    uart1_msg_send(buf,(num+1+1+2+2));
}



/***************自定义保存上报**********************/
void custom_save_report()
{
    u16 num=0;
    u8 buf[1024];
    buf[0]=0xA5;
    memcpy(&buf[3],buf_device,16);
    num+=16;
    //MI
    u32 mess =get_message();
    memcpy(&buf[19],&mess,4);
    num+=4;
    //指令码
    u32 code = CUSTOM_SAVE;
    memcpy(&buf[23],&code,4);
    num+=4;
    memcpy(&buf[1],&num,2);
    //CRC
    buf[27]=0;
    buf[28]=0;

    buf[29]=0x5a;
    TCP_send_data(buf,(num+1+1+2+2));
    uart1_msg_send(buf,(num+1+1+2+2));
}


/*****************心跳包*************************/
void heart_beat_handle()
{
    u8 heart_buffer[SIZE];
    u16 j=0;
    memcpy(&heart_buffer[1+2],buf_device,16);  //DN
    j +=16;
    u32 mess = get_message();
    memcpy(&heart_buffer[1+16+2],&mess,4);
    j +=4;
    u32 code = HEARTBEAT_PACKET;
    memcpy(&heart_buffer[1+16+2+4],&code,4);
    j +=4;
    memcpy(&heart_buffer[1],&j,sizeof(u16));

    j+= sizeof(u16);
    heart_buffer[0]= 0xA5;
    j +=1;
    heart_buffer[j] =0;
    j +=1;
    heart_buffer[j] =0;
    j +=1;
    heart_buffer[j] =0x5A;
    j +=1;
    TCP_send_data(heart_buffer,j);
}

/*主模式变换上报*/
void change_main_mode_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
}

/*方向改变上报*/
void change_light_direction_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
}


/*灯光亮度变换上报*/
void change_RGB_brighness_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
}

/*场景模式子模式变换上报*/
void change_scene_mode_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
}

/*场景模式子模式灵敏度上报*/
void change_scene_senet_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
}

/*自定义模式自模式上报*/
void change_custom_ssp_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
}


/*自定义模式 预览分段长度及内容*/
void change_custom_part_handle(u32 code,PART_INFO_T part_info)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = part_info.len; 
    send_num +=1;
    memcpy(&buffer[send_num],part_info.RGB,45);
    send_num +=45;
}

/*自定义模式 预览 等待时间*/
void change_custom_wait_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
}

/*自定义模式 效果模式*/
void change_light_effect_ssp_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
}

/*麦克风灵敏度*/
void change_micro_senst_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
}


/*麦克风子模式*/
void change_spectral_mode_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
}

/*分段模式*/
void change_part_info_handle(u32 code,PART_INFO_T part_info)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = part_info.len; 
    send_num +=1;
    memcpy(&buffer[send_num],part_info.RGB,45);
    send_num +=45;
}
